package v1

import (
	"yuanxin-admin/internal/repository"
	"yuanxin-admin/internal/vo"

	"gitee.com/botaoshow/yuanxin/web/response"
	"github.com/gin-gonic/gin"
)

type SysParamsController struct {
}

func NewSysParamsController() *SysParamsController {
	return &SysParamsController{}
}

func (s *SysParamsController) Query(c *gin.Context) {
	var list []*vo.ConfigParams
	err := repository.SysConfig.WithContext(c).
		Select(repository.SysConfig.KeyName.As("key"),
			repository.SysConfig.SettingValue.As("value"),
			repository.SysConfig.Info.As("remark")).
		Order(repository.SysConfig.ID.Asc()).
		Scan(&list)
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	response.Success(c, list)
}

func (s *SysParamsController) Sync(c *gin.Context) {
	var d vo.ConfigParams
	err := c.ShouldBind(&d)
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	r, err := repository.SysConfig.WithContext(c).Where(repository.SysConfig.KeyName.Eq(d.Key)).
		Update(repository.SysConfig.SettingValue, d.Value)
	if err != nil {
		response.Fail(c, err.Error())
		return
	}
	response.ToAjax(c, r.RowsAffected > 0)

}
